index = {
	useFiles : [ 'uikit', 'uikit.css', 'easyui', 'easyui.css',
			'easyui-icon.css', 'page.css', 'easyui-locale' ],
	initPage : function() {
		// 顶部导航
		$(document).on("click", '.top-nav a', index.topNavClick);

		// 左侧导航
		$(document).on("click", '.left-nav a.uk-button', index.leftNavClick);

		// 上下文菜单
		$(document).on("contextmenu", '.tabs-inner', index.tab_contextmenu);

		// 刷新
		$(document).on("click", '#mm-tabUpdate', index.mmTabUpdate_click);

		// 关闭当前
		$(document).on("click", '#mm-tabClose', index.mmTabClose_click);

		// 全部关闭
		$(document).on("click", '#mm-tabCloseAll', index.mmTabCloseAll_click);

		// 关闭除当前之外的TAB
		$(document).on("click", '#mm-tabCloseOther',
				index.mmTabCloseOther_click);

		// 关闭当前右侧的TAB
		$(document).on("click", '#mm-tabCloseRight',
				index.mmTabCloseRight_click);

		// 关闭当前左侧的TAB
		$(document).on("click", '#mm-tabCloseLeft', index.mmTabCloseLeft_click);

		index.refreshIndexTop();
	},
	refreshIndexTop : function() {
		var url = cardone.webContextUrl + '/index/top.html';

		var parentId = $('.top-nav li.uk-active a').attr('id');

		var data = {
			parentId : parentId,
			time : new Date().getTime()
		};

		$('.top-nav').load(url, data, index.refreshIndexLeft);
	},
	refreshIndexLeft : function() {
		var url = cardone.webContextUrl + '/index/left.html';

		var $a = $('.top-nav li.uk-active a');

		var parentId = $a.attr('id');

		var data = {
			parentId : parentId,
			time : new Date().getTime()
		};

		$('.left-nav').load(url, data, function() {
			$('.left-nav div.easyui-accordion').accordion();
		});
	},
	tabsBeforeClose : function(title) {
		var tab = $('#tabs').tabs('getTab', title);

		$(tab.panel('options').content).attr('src', 'about:blank');

		return true;
	},
	mmTabUpdate_click : function() {
		var currentTab = $('#tabs').tabs('getSelected');

		var url = $(currentTab.panel('options').content).attr('src');

		$('#tabs').tabs('update', {
			tab : currentTab,
			options : {
				content : index.createFrame(url)
			}
		});
	},
	mmTabClose_click : function() {
		var currentTabTitle = $('#mm').data("currentTabTitle");

		$('#tabs').tabs('close', currentTabTitle);
	},
	mmTabCloseAll_click : function() {
		$('.tabs-inner span').each(function(i, n) {
			var t = $(n).text();

			$('#tabs').tabs('close', t);
		});
	},
	mmTabCloseOther_click : function() {
		var $siblings = $('.tabs-selected').siblings();

		if ($siblings.length == 0) {
			return false;
		}

		$siblings.each(function(i, n) {
			var t = $('a:eq(0) span', $(n)).text();

			$('#tabs').tabs('close', t);
		});

		return false;
	},
	mmTabCloseRight_click : function() {
		var $nextAll = $('.tabs-selected').nextAll();

		if ($nextAll.length == 0) {
			return false;
		}

		$nextAll.each(function(i, n) {
			var t = $('a:eq(0) span', $(n)).text();

			$('#tabs').tabs('close', t);
		});

		return false;
	},
	mmTabCloseLeft_click : function() {
		var $prevAll = $('.tabs-selected').prevAll();

		if ($prevAll.length == 0) {
			return false;
		}

		$prevAll.each(function(i, n) {
			var t = $('a:eq(0) span', $(n)).text();

			$('#tabs').tabs('close', t);
		});

		return false;
	},
	tab_contextmenu : function(event) {
		$('#mm').menu('show', {
			left : event.pageX,
			top : event.pageY
		});

		var subTitle = $(this).children(".tabs-closable").text();

		$('#mm').data("currentTabTitle", subTitle);

		$('#tabs').tabs('select', subTitle);

		return false;
	},
	topNavClick : function(event) {
		var jq = $(this);

		var isActive = jq.parent().is('.uk-active');

		if (isActive == true) {
			return false;
		}

		jq.parent().addClass('uk-active').siblings().removeClass('uk-active');

		index.refreshIndexLeft();

		return false;
	},
	leftNavClick : function(event) {
		var url = $(this).attr('href');

		var name = $(this).text();

		index.addTabs({
			url : url,
			name : name
		});

		return false;
	},
	addTabs : function(data) {
		if ($('#tabs').tabs('exists', data.name)) {
			$('#tabs').tabs('select', data.name);

			return false;
		}

		var content = index.createFrame(data.url);

		$('#tabs').tabs('add', {
			title : data.name,
			content : content,
			closable : true
		});

		return false;
	},
	createFrame : function(url) {
		var s = '<iframe class="fd-iframe-1-1" src="' + url + '"></iframe>';

		return s;
	}
};

seajs.use(index.useFiles, index.initPage);